<?php
include_once 'app/until/GFW_database.php';

class Db_user_page_visitor extends Model_Db_Core {

	private $con = null;
	private $username;

	function __construct(){

	}

	public function setVisitor($user_id,$page_id,$url,$visitor = NULL){		
		parent::setInsert(__CLASS__);
		 	 	
		$this->value['user_id'] = $user_id;
		$this->value['page_id'] = $page_id;
		$this->value['url'] = $url;
		$this->value['ip'] = $_SERVER['REMOTE_ADDR'];
		$this->value['visitor_id'] = $visitor;
		$this->value['date'] = "NOW()";
		$this->value['time'] = "NOW()";
				
	    $status = $this->writeToDb();
	   
	    return $status;
	}
	public function getVisitorsForPageByDate($date,$page_id){
	    $sql = "SELECT page_id, count(*) as Visitors
                FROM `user_page_visitoruser_page_visitor`
                WHERE `date` = ".$date."
                GROUP BY date
                having page_id =".$page_id;
	   
		parent::setSqlManuel($sql);		
	    $result = parent::result(1);
	    
	    return $result['Visitors'];
	}
    public function getVisitorsTotalByUserId($user_id){
	    $sql = "SELECT count(*) as Visitors
                FROM `user_page_visitor`
                WHERE `user_id` = ".$user_id."
                GROUP BY user_id";
	    
	    parent::setSqlManuel($sql);		
	    $result = parent::result(1);
	    
	    if(isset($result->Visitors)){	        
	        return $result->Visitors;
	    }else{
	        return false;
	    }
	}
 	public function getVisitorsTotalByUserIdStatistic($user_id){
 		$final["total"] = 0;
	    $final["today"] = 0;
	    
	    $sql = "SELECT count(*) as Total
                FROM `user_page_visitor`
                WHERE `user_id` = ".$user_id."
                GROUP BY user_id";
	    
	    parent::setSqlManuel($sql);		
	    $total = parent::result(1);
	    
	    $sql = "SELECT count(*) as Today
                FROM `user_page_visitor`
                WHERE `user_id` = ".$user_id." and `date` = CURDATE()
                GROUP BY user_id";
	    
	    parent::setSqlManuel($sql);		
	    $today = parent::result(1);
	    
	    if(isset($total->Total))
	    	$final["total"] = $total->Total;
	    if(isset($today->Today))
	    	$final["today"] = $today->Today;
	            
	    return $final;
	}
    public function getVisitorsForPageTotal($user_id,$page_id){
	    $sql = "SELECT page_id, count(*) as Visitors
                FROM `user_page_visitor`
                WHERE `date` = ".$user_id."
                GROUP BY page_id
                having page_id =".$page_id;
	    
	    parent::setSqlManuel($sql);		
	    $result = parent::result(1);
	    
	    return $result['Visitors'];
	}

}

?>